﻿using System;
using System.Runtime.Serialization.Formatters;
using UnityEngine;
using System.Collections;

public class Control : MonoBehaviour {

    private VisionControl _visionControl;
    private Analog _analogInput;
    private TouchPlate _touchInput;
    public GameObject Shadow;
    public GameObject Model;
    public SoundHandler Sounds;
    public GameObject Puff;
    private Animator _animator;
	private PlayerControl _playerControl;

    // Use this for initialization
	void Start () {
	    _visionControl = GetComponentInChildren<VisionControl>();
	    _analogInput = Analog.Analogs[0];
	    _touchInput = TouchPlate.Main;
	    _animator = Model.GetComponent<Animator>();
		_playerControl = GetComponent<PlayerControl>();
	    BlinkTime = Time.time;
	}

    private Vector2 _movement;
    private bool _notIdle;
    public float BlinkTime;
    public float MoveAmount = 5f;
    public float BlinkLenght = 30f;
    public float BlinkCooldown = 2f;
	// Update is called once per frame
	void Update () {
	    _notIdle = false;
	    _movement = Vector2.zero;
	    var hInput = _analogInput.Current.x;
	    var vInput = _analogInput.Current.y;
//	    var hInput = Input.GetAxis("Horizontal");
	    _movement.x = hInput*MoveAmount * Time.deltaTime;
        if(_movement != Vector2.zero) {
            PlayAnimation("walk");
            if(!Sounds.walking.isPlaying) Sounds.ToggleWalking(true);
            rigidbody2D.AddForce(_movement);

            var facing = Model.transform.localScale;
            var x = Mathf.Abs(facing.x);
            facing.x = hInput > 0 ? -x : x;
            Model.transform.localScale = facing;

            var deg = Mathf.Lerp(180 * Mathf.Sign(vInput), 0, Mathf.InverseLerp(-1, 1, hInput));
            _visionControl.Rotate(deg);
        }

        if(_touchInput.SwipeEnd) {
            if(Time.time > BlinkTime + BlinkCooldown) {
                BlinkTime = Time.time;
                Instantiate(Puff, transform.position, transform.rotation);
                Blink(_touchInput.Swipe);
                PlayAnimation("attackRun");
                Sounds.PlayBlink();
            }
            Shadow.SetActive(false);
            _touchInput.Swipe = Vector2.zero;
            _touchInput.SwipeEnd = false;
        } else if (_touchInput.Swiping) {
            if (Time.time > BlinkTime + BlinkCooldown) {
                Shadow.SetActive(true);
                ShadowBlink(_touchInput.Swipe);
            }
        }
	    if (!_notIdle) {
	        _animator.Play("idle");
            Sounds.ToggleWalking(false);
	    }
	}

    private void PlayAnimation(string name) {
        _animator.Play(name);
        _notIdle = true;
    }

    void Blink(Vector2 direction) {
        var hit = Physics2D.Raycast(transform.position, direction, BlinkLenght);
        Vector2 destination;
        if (hit.point != Vector2.zero) {
            destination = hit.point;
			var remoteOpponent = hit.collider.GetComponent<RemotePlayer>();
            if (remoteOpponent != null) {
                _playerControl.Attack(remoteOpponent);
                Sounds.PlayKill();
            }
        } else {
            destination = (Vector2)transform.position + (direction*BlinkLenght);
        }
        Debug.DrawLine(transform.position, destination, Color.green, 5f);
        transform.position = destination;
    }

    void ShadowBlink(Vector2 direction) {
        var hit = Physics2D.Raycast(transform.position, direction, BlinkLenght);
        Vector2 destination;
        if(hit.point != Vector2.zero) {
            destination = hit.point;
        } else {
            destination = (Vector2)transform.position + (direction * BlinkLenght);
        }
        Shadow.transform.position = destination;
    }
}
